package j20251112;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo6 {
    public static void main(String[] args) throws Exception {
        // 使用 Lambda 表达式创建 Callable
        Callable<Integer> callable = () -> {
            int sum = 0;
            for (int i = 1; i <= 100; i++) {
                sum += i;
                Thread.sleep(10); // 模拟计算过程
            }
            return sum;
        };

        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        new Thread(futureTask).start();

        // 可以在这里执行其他任务...
        System.out.println("等待计算结果中...");

        Integer result = futureTask.get();
        System.out.println("1-100的和为: " + result); // 输出: 5050
    }
}
